package com.bjsxt.userscenter.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.controller.BaseController;
import com.bjsxt.userscenter.common.util.ConstatFinalUtil;
import com.bjsxt.userscenter.common.util.EncryptUtil;
import com.bjsxt.userscenter.users.pojo.AUsers;
import com.bjsxt.userscenter.users.service.IUsersService;

/**
 * 用户登陆后的Controller
 * 
 * @author wangshSxt
 *
 */
@Controller
@RequestMapping("/head/users")
public class UsersHeadController extends BaseController
{
	@Resource
	private IUsersService usersService;
	/**
	 * 登陆后的首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.LOGGER.info("--- main ---");
		return "/head/users/main";
	}
	
	/**
	 * 用户修改页面
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("--- usersUpdate ---");
		HttpSession session= request.getSession() ;
		AUsers users = (AUsers) session.getAttribute("users");
		
		/* 根据ID查询用户 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", users.getId()); 
		condMap.put("extend", "true");
		users = this.usersService.findOneUsersService(condMap);
		request.setAttribute("users", users);
		return "/head/users/usersUpdate" ; 
	}
	
	/**
	 * 用户修改提交页面
	 * @return
	 */
	@RequestMapping("/usersUpdateSubmit")
	public String usersUpdateSubmit(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("--- usersUpdateSubmit ---");
		HttpSession session= request.getSession() ;
		AUsers users = (AUsers) session.getAttribute("users");
		
		/* 根据ID查询用户 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", users.getId()); 
		users = this.usersService.findOneUsersService(condMap);
		
		/* 操作标识 */
		String operType = request.getParameter("operType");
		
		/* 断言 */
		boolean updateFlag = false ; 
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 更新个人资料 */
			String qq = request.getParameter("qq");
			String address = request.getParameter("address");
			String regionId = request.getParameter("regionId");
			
			users.setRegionId(Integer.valueOf(regionId));
			users.setQq(qq);
			users.setAddress(address);
			updateFlag = true ; 
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			/* 修改密码 */
			String souPass = request.getParameter("souPass");
			String password = request.getParameter("password");
			
			if(EncryptUtil.checkPass(souPass, users.getPassword()))
			{
				users.setPassword(EncryptUtil.encodeStr(password));
				updateFlag = true ; 
			}else
			{
				this.info = "原密码不正确" ; 
			}
		}else
		{
			this.info = "您想干啥?" ; 
		}
		
		if(updateFlag)
		{
			users.setUpdateTime(new Date());
			JSONObject resDbJSON = this.usersService.updateOneUsersService(users);
			this.info = resDbJSON.getString("info");
		}
		request.setAttribute("info", info);
		/*return this.usersUpdate(request) ; */
		return this.usersUpdate(request) ; 
	}
	
	/**
	 * 退出
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("---- logout ----");
		/* 将session中的信息删除 */
		session.removeAttribute("users");
		session.removeAttribute("lastLoginTime");
		
		/* 操作Token */
		String token = request.getParameter("token");
		if(token == null)
		{
			token = "" ; 
		}
		if(!"".equalsIgnoreCase(token))
		{
			ConstatFinalUtil.TOKENS_MAP.remove(token);
		}
		
		this.info = ConstatFinalUtil.INFO_JSON.getString("8") ;
		/*request.setAttribute("info", info);
		return "/back/login" ; 
		*/
		session.setAttribute("info", info);
		return "redirect:/login.htm" ; 
	}
}
